package com.xydtech.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.exception.ApiException;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import com.xydtech.system.service.ISysWechatAppletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 创建时间：2021/11/4 9:13
 * 项目名称：xydtech
 * @author JHON
 * @version 1.0
 * @since JDK 1.8.0_201
 * 文件名称：DataPlatHttpUtils
 * 类说明：调用数据平台公共接口类

 */
@Component
public class DataPlatHttpUtils {
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysWechatAppletService sysWeChatAppletService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Value("${url.apiServiceUrl}")
    private String apiServiceUrl;

    private static Logger log = LoggerFactory.getLogger(DataPlatHttpUtils.class);
    /**
     *  调用数据平台公共接口
     * @Title: dataPlatRequest
     * @param inTerFaceNum   数据平台接口编号
     * @param appId  小程序/公众号/子系统appid
     * @param appletType 子系统类型
     * @param deptId 法人机构编号
     * @param paramMap
     * @return com.xydtech.common.model.ResponseModel
     * @exception
     * @author JHON
     * @Date 2021/11/4 9:35
     **/
    public ResponseModel dataPlatRequest(String inTerFaceNum, String appId, String appletType, Long deptId, Map<String, Object> paramMap) throws ApiException {
        SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        String name = String.valueOf(paramMap.get("name"));
        String idCardNumber = String.valueOf(paramMap.get("idCardNumber"));
        SysWechatApplet sysWechatApplet = null;
        if (!appId.equals("")) {
            sysWechatApplet = sysWeChatAppletService.getWechatAppletByAppId(appletType, appId);
        } else {
            sysWechatApplet = sysWeChatAppletService.getWechatAppletByDeptId(appletType, deptId);
        }
        if (sysWechatApplet == null) {
            throw new ApiException(ResultStatus.APPLET_CONFIG_IS_MISSING);
        }
        // 数据平台Url
        String url = configService.selectConfigByKey("dataPlat.url", dept.getDeptId());
        RestTemplate restTemplate = new RestTemplate();
        //2、添加请求头
        HttpHeaders headers = new HttpHeaders();
        //3.body
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("url", url);
        map.add("deptId", deptId);
        map.add("subSysCode", sysWechatApplet.getSubSysCode());
        map.add("subSysToken", sysWechatApplet.getSubSysToken());
        map.add("interfaceNum", inTerFaceNum);
        map.add("name", name);
        map.add("idCardNumber", idCardNumber);
        HttpMethod method = HttpMethod.POST;
        //3.执行HTTP请求
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
        //调用数据平台，改为bigdata请求
        String apiUrl = apiServiceUrl + "/api/dataPlat/getDataPlatRequest";
        log.info("============apiUrl" + apiUrl);
        log.info("============参数：" + url+";"+
                deptId+";"+
                sysWechatApplet.getSubSysCode()+";"+
                sysWechatApplet.getSubSysToken()+";"+
                inTerFaceNum+";"+
                name+";"+
                idCardNumber);
        ResponseEntity response = restTemplate.exchange(apiUrl, method, requestEntity, String.class);
        JSONObject body = JSON.parseObject(String.valueOf(response.getBody()));
        String code = body.getString("code");
        JSONObject jsonObject = JSON.parseObject(body.getString("data"));
        log.info("============jsonObject：" + jsonObject+";");
        if ("100".equals(code)) {
            return JSON.toJavaObject(jsonObject, ResponseModel.class);
        }else {
            return ResponseModel.error("获取企业信息失败！");
        }
    }
}